﻿'use strict'

var spaceDockApp = {};

spaceDockApp.ships = [];


 
//Create Table
spaceDockApp.disembark = function () {
    var request = new XMLHttpRequest();
    request.open ('GET', 'https://coralsspacedock.firebaseio.com/.json', false);
    request.onload = function () {
        var data = JSON.parse(this.response);
        for (var m in data) {
            data[m].id = m;
            spaceDockApp.ships.push(data[m]);
        }

        var h = '<table class="table table-bordered table-striped">';
        h += '<tr>';
        h += '<th>Ship Name</th>';
        h += '<th>Ship Type</th>';
        h += '</th></th>';
        h += '<th></th>';
        h += '<th></th>';
        h += '</tr>';

        for (var index in spaceDockApp.ships) {

            var ship = spaceDockApp.ships[index];

            h += '<tr>';
            h += '<td>' + ship.name + '</td>';
            h += '<td>' + ship.type + '</td>';
            h += "<td><img src='" + ship.pic + "' class='img-responsive img-thumbnail ship-image'</td>";
            h += '<td><button onclick="spaceDockApp.editShips(' + index + ')">Edit</button>';
            h += '<td><button onclick="spaceDockApp.launchFromDock(' + index + ')">Launch</button>';
            h += '</tr>';
        }

        h += '</table>';

        document.getElementById('dockTable').innerHTML = h;
    }

    request.send();   
}

//Add Ship to Array
spaceDockApp.dockShip = function () {

    var name = document.getElementById('shipName');
    var type = document.getElementById('shipType');
    var pic = document.getElementById('shipPic');

    var newShip = {
        name: name.value,
        type: type.value,
        pic: pic.value,
    };

    var request = new XMLHttpRequest();
    request.open('Post', 'https://coralsspacedock.firebaseio.com/.json', false);
    request.onload = function () {
        var data = JSON.parse(this.response);
        newShip.id = data.name;

        spaceDockApp.ships.push(newShip);

        name.value = type.value = pic.value = '';

        spaceDockApp.disembark();
    }

    var newShipAsStr = JSON.stringify(newShip);
    request.send(newShipAsStr);
}

//Delete Ship
spaceDockApp.launchFromDock = function (index) {
    var ship = this.ships[index];

    if (confirm('Are you sure you are ready to leave dock?')) {
        var request = new XMLHttpRequest();
        request.open('DELETE', 'https://coralsspacedock.firebaseio.com/' + ship.id + '.json', false)
        request.send();

        spaceDockApp.ships.splice(index, 1); 

        spaceDockApp.disembark();
    }
}

//Edit Ship Info
spaceDockApp.editShips = function (index) {
    var ship = this.ships[index];

    var name = document.getElementById('shipName');
    var type = document.getElementById('shipType');
    var pic = document.getElementById('shipPic');

    name.value = ship.name;
    type.value = ship.type;
    pic.value = ship.pic;

    this.disembark();
}

spaceDockApp.disembark();